package com.zzq.controller;

import com.zzq.pojo.Blog;
import com.zzq.service.blog.BlogService;
import com.zzq.service.user.UserService;
import com.zzq.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("/blog/write")
    public String writeBlog(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Object a = request.getSession().getAttribute(Constants.USERNAME_SESSION);
        String userName = (String)a;
        String blog = request.getParameter("blog");
        String blogName = request.getParameter("blogName");
        /*io*/
        File dir = new File("D:/blogs/"+userName+"/");
        if(!dir.exists()){dir.mkdir();}
        File F = new File("D:/blogs/"+userName+"/"+blogName+".txt");
        if(!F.exists()){F.createNewFile();}
        FileWriter fw=null;
        String writeDate=blog;
        fw=new FileWriter(F, false);
        fw.write(writeDate+"\r\n");
        fw.close();
        /*ioEnd*/
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        BlogService blogServiceImpl = (BlogService) context.getBean("blogServiceImpl");
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        blogServiceImpl.addBlog(blogName,userName,formatter.format(date));
        return "login";
    }

    @RequestMapping("/blog/getBlog")
    public String browseBlog(Model model){
        List<Blog> blogs = blogService.getAllBlog();
        model.addAttribute("blogs",blogs);
        return "browseBlog";
    }

    @RequestMapping("/blog/getUserBlog/{userName}")
    public String getUserBlog(@PathVariable("userName")String userName, Model model){
        List<Blog> blogs = blogService.getUserBlog(userName);
        model.addAttribute("blogs",blogs);
        return "browseBlog";
    }

    @RequestMapping("/blog/getBlog/{id}/{blogName}")
    public String getBlog(@PathVariable("id")String userId,@PathVariable String blogName,Model model){
        System.out.println(userId+blogName);
        String FileName="D:/blogs/"+userId+"/"+blogName+".txt";
        File myFile=new File(FileName);
        if(!myFile.exists())
        {
            /*System.err.println("Can't Find " + FileName);*/
            System.out.println("no");
        }

        try
        {
            BufferedReader in = new BufferedReader(new FileReader(myFile));
            String str;
            while ((str = in.readLine()) != null)
            {
                model.addAttribute("blogName",blogName);
                model.addAttribute("blogContent",str);
                System.out.println(str);
                return "readBlog";
            }
            in.close();
        }
        catch (IOException e)
        {
            e.getStackTrace();
        }



        return "main";
    }


}
